package aom.ju.ss.view.base.jzvd;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.SeekBar;

import com.danikula.videocache.HttpProxyCacheServer;

import aom.ju.ss.R;
import aom.ju.ss.utils.BaseApplication;
import aom.ju.ss.utils.LogUtils;


public class JYVideoPlayer extends JzvdStd {



    private Context context;
    private OnClickListener mOnClickListener;

    public JYVideoPlayer(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public void onProgress(int progress, long position, long duration) {
        super.onProgress(progress, position, duration);
        // System.out.println("----------短视频onProgress="+progress);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        super.onProgressChanged(seekBar, progress, fromUser);
        //System.out.println("-----------短视频onProgressChanged="+progress);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    public void onVideoSizeChanged(int width, int height) {
        super.onVideoSizeChanged(width, height);
        // System.out.println("---------onVideoSizeChanged--短视频 width="+width);
        // System.out.println("---------onVideoSizeChanged--短视频 height="+height);
        if (width > height) {
            Jzvd.setVideoImageDisplayType(Jzvd.VIDEO_IMAGE_DISPLAY_TYPE_ORIGINAL);
        } else {
            Jzvd.setVideoImageDisplayType(Jzvd.VIDEO_IMAGE_DISPLAY_TYPE_FILL_SCROP);
        }
    }

    public JYVideoPlayer(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    @Override
    public void onAutoCompletion() {
        thumbImageView.setVisibility(View.GONE);
        //  posterImageView.setVisibility(View.GONE);
//        if (screen == Jzvd.SCREEN_FULLSCREEN) {
//            onStateAutoComplete();
//            setUp(jzDataSource, Jzvd.SCREEN_FULLSCREEN);
//        } else {
//            super.onAutoCompletion();
//            setUp(jzDataSource, Jzvd.STATE_NORMAL);
//        }
        super.onAutoCompletion();
        setUp(jzDataSource, Jzvd.SCREEN_FULLSCREEN);
        //循环播放

        startVideo();
    }


    @Override
    public void setUp(String url, String title, int screen) {
        if (url.startsWith("http")) {
            HttpProxyCacheServer proxy = BaseApplication.getProxy(context);
            String proxyUrl = proxy.getProxyUrl(url);
            super.setUp(proxyUrl, title, screen);
        } else {
        super.setUp(url, title, screen);
        }
    }

    @Override
    public void init(final Context context) {
        super.init(context);
        resetPlayView();
    }


    @Override
    public void onClick(View v) {
        if (isPlay()) {
            goOnPlayOnPause();
            if (mOnClickListener != null) {
                mOnClickListener.onPlayOnPause();
            }
        } else {
            //暂停
            if (state == STATE_PAUSE) {
                goOnPlayOnResume();
                if (mOnClickListener != null) {
                    mOnClickListener.onPlayOnResume();
                }
            } else {
                startVideo();
            }
        }
        resetPlayView();
    }

    @Override
    public void startVideo() {
        try {
            //        }
            if (!isPlay()) {
                //暂停
                if (state == STATE_PAUSE) {
                    goOnPlayOnResume();
                    if (mOnClickListener != null) {
                        mOnClickListener.onPlayOnResume();
                    }
                } else {
                    super.startVideo();
                    if (mOnClickListener != null) {
                        mOnClickListener.onstartVideo();
                    }
                }
            }
            resetPlayView();
        }catch (Exception e){
            LogUtils.e(e.getMessage());
        }
//        if (screen == SCREEN_FULLSCREEN) {
//            addTextureView();
//            AudioManager mAudioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
//            mAudioManager.requestAudioFocus(onAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
//            JZUtils.scanForActivity(getContext()).getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//
////            JZMediaManager.setDataSource(dataSourceObjects);
////            JZMediaManager.setCurrentDataSource(JZUtils.getCurrentFromDataSource(dataSourceObjects, currentUrlMapIndex));
////            JZMediaManager.instance().positionInList = positionInList;
//            onStatePreparing();
//            startLayout.setVisibility(VISIBLE);
//        } else {
//            super.startVideo();
//            startLayout.setVisibility(GONE);

    }

    private void resetPlayView() {
//        if (isPlay()) {
//            startView.setBackgroundResource(R.mipmap.video_play_parse);
//        } else {
//            startView.setBackgroundResource(R.mipmap.stop);
//        }
    }

    /**
     * 是否播放
     *
     * @return
     */
    public boolean isPlay() {
        return state == STATE_PREPARED || state == STATE_PLAYING || state == -1;
    }

    public JYVideoPlayer setOnClickListener(OnClickListener onClickListener) {
        mOnClickListener = onClickListener;
        return this;
    }

    public interface OnClickListener {
        void onPlayOnPause();

        void onPlayOnResume();

        void onstartVideo();
    }

    @Override
    public void onStatePause() {
        super.onStatePause();
    }

}